/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Model.Category;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Admin
 */
public class dialogEditCategory extends javax.swing.JDialog {

    /**
     * Creates new form dialogNewCategory
     */
    ObjectOutputStream out;
    ObjectInputStream in;  
 //   static String oldCategory;
    Category cateEdit = new Category();

    public dialogEditCategory(java.awt.Frame parent, boolean modal,  Category oldCate,ObjectOutputStream out, ObjectInputStream in) {
        super(parent, modal);
       
        this.cateEdit = oldCate;
        initComponents();
        txtOldCategory.setText(cateEdit.getCategoryName());
        this.in =in;this.out =out;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtNewCategory = new javax.swing.JTextField();
        btnOKCategory = new javax.swing.JButton();
        btnCancelCategory = new javax.swing.JButton();
        lbCategory = new javax.swing.JLabel();
        txtOldCategory = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Category");
        setResizable(false);

        txtNewCategory.setText("Typing new category");

        btnOKCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonOK32px.png"))); // NOI18N
        btnOKCategory.setText("OK");
        btnOKCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKCategoryActionPerformed(evt);
            }
        });

        btnCancelCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonCancel.png"))); // NOI18N
        btnCancelCategory.setText("Cancel");
        btnCancelCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelCategoryActionPerformed(evt);
            }
        });

        lbCategory.setText("Edit Category");

        txtOldCategory.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(btnOKCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelCategory))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtOldCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(16, 16, 16))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbCategory)
                                .addGap(76, 76, 76))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtNewCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(16, 16, 16)))))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(lbCategory)
                .addGap(18, 18, 18)
                .addComponent(txtOldCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(txtNewCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOKCategory)
                    .addComponent(btnCancelCategory))
                .addGap(52, 52, 52))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKCategoryActionPerformed
        try {
            // TODO add your handling code here:
            String categoryName = txtNewCategory.getText();
           cateEdit.setCategoryName(categoryName);
           // Category cateEdit = new Category(txtOldCategory.getText());
            out.writeObject("editCategory");
            out.writeObject(cateEdit);
            boolean alert  = (boolean)in.readObject();
            if (alert) {
                JOptionPane.showMessageDialog(this, "Edit new category success");
                this.setVisible(false);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Edit new category failed");
            }
        } catch (ClassNotFoundException | IOException ex) {
            System.out.println(ex);
        }
    }//GEN-LAST:event_btnOKCategoryActionPerformed

    private void btnCancelCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelCategoryActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_btnCancelCategoryActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelCategory;
    private javax.swing.JButton btnOKCategory;
    private javax.swing.JLabel lbCategory;
    private javax.swing.JTextField txtNewCategory;
    private javax.swing.JTextField txtOldCategory;
    // End of variables declaration//GEN-END:variables
}
